package com.zwb.boot.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zwb.boot.common.R;
import com.zwb.boot.entity.Orders;
import com.zwb.boot.service.OrdersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;

/**
 * @author 头发又黑又长
 * @Date 2022/7/27 14:03
 * @email zwb15083976291@163.com
 */
@Slf4j
@Api("订单详情管理")
@RestController
@RequestMapping("/orderAdministration")
public class orderAdministrationController {

    @Autowired
    private OrdersService ordersService;

    @ApiOperation("订单分页查询")
    @GetMapping("/page")
    public R<Page<Orders>> page(@RequestParam(required = false, defaultValue = "1") Integer page,
                                @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                @RequestParam(required = false) Integer number,
                                @RequestParam(required = false) Date beginTime,
                                @RequestParam(required = false) Date endTime) {

        Page<Orders> ordersPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .like(ObjectUtils.isNotEmpty(number), Orders::getNumber, number) // 查询订单号
                .ge(ObjectUtils.isNotEmpty(beginTime), Orders::getOrderTime, beginTime) // 查询 最小时间
                .le(ObjectUtils.isNotEmpty(endTime), Orders::getOrderTime, endTime) // 查询最大时间
                .orderByDesc(Orders::getOrderTime);
        ordersService.page(ordersPage, queryWrapper);
        return R.success(ordersPage);
    }


    @ApiOperation("修改订单状态")
    @PutMapping
    public R<String> updateStatus(@RequestBody Orders orders) {
        if (!Arrays.asList(1, 2, 3, 4, 5).contains(orders.getStatus())) {
            return R.success("错误的状态码");
        }
        if (ObjectUtils.isEmpty(orders.getId())) {
            return R.success("缺少关键参数");
        }
        LambdaUpdateWrapper<Orders> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Orders::getNumber, orders.getId())
                .set(Orders::getStatus, orders.getStatus());
        ordersService.update(updateWrapper);
        return R.success("修改状态成功");
    }

}
